// 下载音视频文件
import { Loading } from 'element-ui';
import Vue from 'vue';

export const downloadMaterial = (res, name) => {
  const loading = Loading.service({ fullscreen: true });
  fetch(res)
    .then((res) => res.blob())
    .then((blob) => {
      if ('download' in document.createElement('a')) {
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', decodeURIComponent(name));
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
      } else {
        window.navigator.msSaveBlob(blob, decodeURIComponent(name));
      }
      new Vue().$nextTick(loading.close());
    })
    .catch(() => {
      new Vue().$nextTick(loading.close());
    });
};
